package winOrDie.graphicsInterface.Login;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import winOrDie.Game.WoD_Player_Data;

public class Composite_Login extends Composite {

	private Label label_Login = null;
	
	private Composite composite_User = null;
	private Label label_User = null;
	private Text text_User = null;
	
	private Composite composite_Password = null;
	private Label label_Password = null;
	private Text text_Password = null;
	
	private Composite composite_Buttons = null;
	private Button button_LOGIN = null;
	private Button button_LAN = null;
	private Button button_EXIT = null;
	
	private Vector<Composite_Login_Listener> listeners = null;
	
	public synchronized void add_Composite_Login_Listener(Composite_Login_Listener listener)
	{
		listeners.add(listener);
	}

	public synchronized void remove_Composite_Login_Listener(Composite_Login_Listener listener)
	{
		listeners.remove(listener);
	}
	
	protected void fire_Composite_Login_Event(Composite_Login_Event event)
	{
		for (Composite_Login_Listener listener : listeners) {
			listener.on_Composite_Login_Event(event);
		}
	}
	
	public Composite_Login(Composite parent, int style) {
		super(parent, style);
		
		listeners = new Vector<Composite_Login_Listener>();
		
		initialize();
	}
	
	
	private void initialize() {
		setSize(new Point(300, 200));
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		setLayout(gridLayout);
		
		get_Label_Login();
		get_Text_User();
		get_Text_Password();
		get_Composite_Buttons();
	}

	private Label get_Label_Login()
	{
		if (label_Login == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			
			label_Login = new Label(this, SWT.NONE);
			label_Login.setText("Login");
			
			label_Login.setLayoutData(gridData);
		}
		
		return label_Login;
	}
	
	private Text get_Text_User()
	{
		if (text_User == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_User = new GridData();
			gridData_Composite_User.grabExcessHorizontalSpace = true;
			gridData_Composite_User.horizontalAlignment = SWT.FILL;
			
			composite_User = new Composite(this, SWT.NONE);
			composite_User.setLayout(gridLayout);
			composite_User.setLayoutData(gridData_Composite_User);
			
			label_User = new Label(composite_User, SWT.NONE);
			label_User.setText("Usuario:");
			
			GridData gridData_Text_User = new GridData();
			gridData_Text_User.grabExcessHorizontalSpace = true;
			gridData_Text_User.horizontalAlignment = SWT.FILL;
			text_User = new Text(composite_User, SWT.BORDER);
			text_User.setLayoutData(gridData_Text_User);
		}
		
		return text_User;
	}
	
	private Text get_Text_Password()
	{
		if (text_Password == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Password = new GridData();
			gridData_Composite_Password.grabExcessHorizontalSpace = true;
			gridData_Composite_Password.horizontalAlignment = SWT.FILL;
			
			composite_Password = new Composite(this, SWT.NONE);
			composite_Password.setLayout(gridLayout);
			composite_Password.setLayoutData(gridData_Composite_Password);
			
			label_Password = new Label(composite_Password, SWT.NONE);
			label_Password.setText("Password:");
			
			GridData gridData_Text_Password = new GridData();
			gridData_Text_Password.grabExcessHorizontalSpace = true;
			gridData_Text_Password.horizontalAlignment = SWT.FILL;
			text_Password = new Text(composite_Password, SWT.PASSWORD | SWT.BORDER);
			text_Password.setLayoutData(gridData_Text_Password);
		}
		
		return text_Password;
	}
	
	
	private Composite get_Composite_Buttons()
	{
		if (composite_Buttons == null)
		{
			GridLayout gridLayout_Composite_Buttons = new GridLayout();
			gridLayout_Composite_Buttons.numColumns = 3;
			
			GridData gridData_Composite_Buttons = new GridData();
			gridData_Composite_Buttons.grabExcessHorizontalSpace = true;
			gridData_Composite_Buttons.horizontalAlignment = SWT.FILL;
			
			composite_Buttons = new Composite(this, SWT.NONE);
			composite_Buttons.setLayout(gridLayout_Composite_Buttons);
			composite_Buttons.setLayoutData(gridData_Composite_Buttons);
			
			/**
			 * Login button
			 */
			GridData gridData_Button_Login = new GridData();
			gridData_Button_Login.grabExcessHorizontalSpace = true;
			gridData_Button_Login.horizontalAlignment = SWT.CENTER;
			
			button_LOGIN = new Button(get_Composite_Buttons(), SWT.PUSH);
			button_LOGIN.setLayoutData(gridData_Button_Login);
			button_LOGIN.setText("Login");
			
			final Composite_Login compositeLogin = this;
			
			button_LOGIN.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Login_Event event = new Composite_Login_Event(compositeLogin);
					event.setEventType(Composite_Login_Event.Event_Types.LOGIN);
					fire_Composite_Login_Event(event);
				}
			});
		
			
			
			/**
			 * LAN button
			 */
			GridData gridData_Button_LAN = new GridData();
			gridData_Button_LAN.grabExcessHorizontalSpace = true;
			gridData_Button_LAN.horizontalAlignment = SWT.CENTER;
			
			button_LAN = new Button(get_Composite_Buttons(), SWT.PUSH);
			button_LAN.setLayoutData(gridData_Button_LAN);
			button_LAN.setText("LAN");
			
			button_LAN.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Login_Event event = new Composite_Login_Event(compositeLogin);
					event.setEventType(Composite_Login_Event.Event_Types.LAN);
					fire_Composite_Login_Event(event);
				}
			});
			
			
			/**
			 * EXIT button
			 */
			GridData gridData_Button_EXIT = new GridData();
			gridData_Button_EXIT.grabExcessHorizontalSpace = true;
			gridData_Button_EXIT.horizontalAlignment = SWT.CENTER;
			
			button_EXIT = new Button(get_Composite_Buttons(), SWT.PUSH);
			button_EXIT.setLayoutData(gridData_Button_EXIT);
			button_EXIT.setText("Salir");
			
			button_EXIT.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Login_Event event = new Composite_Login_Event(compositeLogin);
					event.setEventType(Composite_Login_Event.Event_Types.EXIT);
					fire_Composite_Login_Event(event);
				}
			});
			
		}
		
		return composite_Buttons;
	}
	
	
	public WoD_Player_Data get_WoD_Player_Data()
	{
		WoD_Player_Data woDPlayerData = new WoD_Player_Data();
		woDPlayerData.setUserName(get_Text_User().getText());
		woDPlayerData.setUserPassword(get_Text_Password().getText());
		return woDPlayerData;
	}
	
}
